package com.etc.controller;

import com.etc.entity.Admin;
import com.etc.entity.User;
import com.etc.service.AdminsService;
import com.etc.util.Layui;
import com.github.pagehelper.PageInfo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/AdminsController")
public class AdminsController {

    Logger logger=Logger.getLogger(AdminsController.class);
    @Autowired
    AdminsService service;

    /**
     * 根据管理员编号修改状态（删除管理员  禁用）
     * 邓玲琪
     * @return boolean int>0 为True  否则为False
     */
    @RequestMapping("/modifyAdminMark")
    @ResponseBody
    public int modifyAdminMark(@RequestParam(value = "adminId")String adminId,
                               @RequestParam(value = "adminMark")int adminMark){
        //调用业务层
        int fian = service.modifyAdminMark(adminId,adminMark);
        return fian;
    }
    /**
     * 按条件模糊查找所有用户信息：用户名，角色
     * 邓玲琪
     * @return List<User>集合
     */
    @RequestMapping("/getqueryAllAdmin")
    @ResponseBody
    public Layui getqueryAllAdmin(@RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
                                  @RequestParam(value = "pageSize",defaultValue = "3")int pageSize,
                                  @RequestParam(value = "adminName",defaultValue = "")String adminName,
                                  @RequestParam(value = "adminPuis",defaultValue = "")int adminPuis){
        PageInfo<Admin>  pageInfo=service.getqueryAllAdmin(pageNum,pageSize,adminName,adminPuis);
        return Layui.data(pageInfo.getTotal(),pageInfo.getList());
    }
    /**
     * 增加管理员
     * 邓玲琪
     * @param
     * @return boolean int>0 为True  否则为False
     */
    @RequestMapping("/addAdmin")
    @ResponseBody
    public int addAdmin(@RequestParam(value = "adminName")String adminName,
                        @RequestParam(value = "adminPwd")String adminPwd,
                        @RequestParam(value = "adminPuis")int adminPuis,
                        @RequestParam(value = "adminMark")int adminMark){
        //调用业务层
        int fian = service.addAdmin(adminName,adminPwd,adminPuis,adminMark);
        return fian;
    }
    /**
     * 初始化密码
     * 邓玲琪
     * @return boolean int>0 为True  否则为False
     */
    @RequestMapping("/InitializeTheAdminPwd")
    @ResponseBody
    public int InitializeTheAdminPwd(@RequestParam(value = "adminId")String adminId) {
        //调用业务层
        int fian = service.InitializeTheAdminPwd(adminId,"123456");
        return fian;
    }
    /**
     * 更改管理员权限
     * 邓玲琪
     * @return boolean int>0 为True  否则为False
     */
    @RequestMapping("/changeAdminPuis")
    @ResponseBody
    public int changeAdminPuis(@RequestParam(value = "adminId")String adminId, @RequestParam(value = "adminPuis")int adminPuis) {
        //调用业务层
        int fian = service.changeAdminPuis(adminId,adminPuis);
        return fian;
    }
    /**
     * 管理员登录
     * 邓玲琪
     */
    @RequestMapping("/login")
    @ResponseBody
    public ModelAndView login(@RequestParam(value = "adminName",defaultValue = "")String adminName,
                              @RequestParam(value = "adminPwd",defaultValue = "")String adminPwd,
                              HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ModelAndView mv=new ModelAndView();
        Admin admin=service.getLogin(adminName,adminPwd);
        if (admin!=null){
            request.getSession().setAttribute("admin",admin);
            return new ModelAndView("back/index");
           /* mv.setViewName("back/index");
            return mv;*/
        }else{
            logger.debug("登录失败");
            mv.setViewName("back/login");
            return mv;
        }
    }
    /**
     * 管理员退出
     * 邓玲琪
     */
    @RequestMapping(value = "/exit")
    @ResponseBody
    public ModelAndView exit(HttpServletRequest request) {
        request.getSession().removeAttribute("admin");
        request.removeAttribute("admin");
        return new ModelAndView("back/login");
    }
    /**
     * 管理员修改密码
     * 邓玲琪
     */
    @RequestMapping("/password")
    @ResponseBody
    public ModelAndView password(@RequestParam(value = "adminPwd",defaultValue = "")String adminPwd,
                                 @RequestParam(value = "adminPwdx",defaultValue = "")String adminPwdx,
                                 HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Admin name=(Admin) request.getSession().getAttribute("admin");
        logger.debug("3333"+name);
        String adminName=name.getAdminName();
        String adminId=name.getAdminId();

        Admin admin=service.getLogin(adminName,adminPwd);
        if (admin!=null){
            int fian = service.InitializeTheAdminPwd(adminId,adminPwdx);
            return new ModelAndView("back/index");
        }else{
            logger.debug("修改失败：请输入正确密码");
            return new ModelAndView("back/index");
        }
    }
}
